﻿using Senparc.Weixin.Cache;
using Senparc.Weixin.Cache.Redis;
using Senparc.Weixin.MP.CommonAPIs;
using Senparc.Weixin.MP.TenPayLib;
using Senparc.Weixin.Threads;

namespace Out.TaskRunner
{
    public class SenparcWeixinConfig
    {
        /// <summary>
        /// 注册 Senparc 微信 
        /// </summary>
        public static void Register()
        {
            RegisterWeixinCache();//注册分布式缓存
            RegisterWeixinThreads();//激活微信缓存（必须）
            RegisterSenparcWeixin();//注册所用微信公众号的账号信息
            RegisterWeixinPay();//注册微信支付

            Senparc.Weixin.Config.IsDebug = false;//这里设为Debug状态时，/App_Data/目录下会生成日志文件记录所有的API请求日志，正式发布版本建议关闭
        }

        /// <summary>
        /// 自定义缓存策略
        /// </summary>
        private static void RegisterWeixinCache()
        {
            //如果留空，默认为localhost（默认端口）
            var redisConfiguration = System.Configuration.ConfigurationManager.AppSettings["Cache_Redis_Configuration"];
            RedisManager.ConfigurationOption = redisConfiguration;

            // 如果不执行下面的注册过程，则默认使用本地缓存
            if (redisConfiguration != "Redis配置")
            {
                RedisContainerCacheStrategy.Instance.CacheSetKey = "gdsfybjywxmp";
                CacheStrategyFactory.RegisterContainerCacheStrategy(() => RedisContainerCacheStrategy.Instance);//Redis
            }
            //CacheStrategyFactory.RegisterContainerCacheStrategy(() => MemcachedContainerStrategy.Instance);//Memcached
        }

        /// <summary>
        /// 激活微信缓存
        /// </summary>
        private static void RegisterWeixinThreads()
        {
            ThreadUtility.Register();
        }

        /// <summary>
        /// 注册所用微信公众号的账号信息
        /// </summary>
        private static void RegisterSenparcWeixin()
        {
            AccessTokenContainer.Register(
                System.Configuration.ConfigurationManager.AppSettings["WeixinAppId"],
                System.Configuration.ConfigurationManager.AppSettings["WeixinAppSecret"]);
        }

        /// <summary>
        /// 注册微信支付
        /// </summary>
        private static void RegisterWeixinPay()
        {
            //提供微信支付信息
            var weixinPayPartnerId = System.Configuration.ConfigurationManager.AppSettings["WeixinPay_PartnerId"];
            var weixinPayKey = System.Configuration.ConfigurationManager.AppSettings["WeixinPay_Key"];
            var weixinPayAppId = System.Configuration.ConfigurationManager.AppSettings["WeixinPay_AppId"];
            var weixinPayAppKey = System.Configuration.ConfigurationManager.AppSettings["WeixinPay_AppKey"];
            var weixinPayTenpayNotify = System.Configuration.ConfigurationManager.AppSettings["WeixinPay_TenpayNotify"];

            //var tenPayV3_MchId = System.Configuration.ConfigurationManager.AppSettings["TenPayV3_MchId"];
            //var tenPayV3_Key = System.Configuration.ConfigurationManager.AppSettings["TenPayV3_Key"];
            //var tenPayV3_AppId = System.Configuration.ConfigurationManager.AppSettings["TenPayV3_AppId"];
            //var tenPayV3_AppSecret = System.Configuration.ConfigurationManager.AppSettings["TenPayV3_AppSecret"];
            //var tenPayV3_TenpayNotify = System.Configuration.ConfigurationManager.AppSettings["TenPayV3_TenpayNotify"];

            var weixinPayInfo = new TenPayInfo(weixinPayPartnerId, weixinPayKey, weixinPayAppId, weixinPayAppKey, weixinPayTenpayNotify);
            TenPayInfoCollection.Register(weixinPayInfo);

            //var tenPayV3Info = new TenPayV3Info(tenPayV3_AppId, tenPayV3_AppSecret, tenPayV3_MchId, tenPayV3_Key,
            //                                    tenPayV3_TenpayNotify);
            //TenPayV3InfoCollection.Register(tenPayV3Info);
        }
    }
}